package com.tian.web;

import com.tian.po.UserPO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HelloController {
    @RequestMapping("/")
    public String index(ModelMap map) {
        // 加入一个属性，用来在模板中读取
        map.addAttribute("host", "http://blog.didispace.com");
        // return模板文件的名称，对应src/main/resources/templates/index.html
        return "index";
    }

    /**
     * 测试Thymeleaf模板引擎
     * @param model
     * @return
     */
    @RequestMapping(value = "/viewUser", method = RequestMethod.GET)
    public String viewUser(Model model){
        UserPO user = new UserPO();
        user.setId(100);
        user.setAge(18);
        user.setName("詹姆斯");
        model.addAttribute("user", user);
        return "user/userView";
    }

    /**
     * 测试统一异常
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/err", method = RequestMethod.GET)
    public String error() throws Exception {
        throw new Exception("发生错误");
    }
}
